package com.hc.comm.Service;

import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;

import com.hc.bean.RoleBean;
import com.hc.comm.CommData;
import com.hc.radius.service.Order;
import com.hc.radius.service.RaduistClient;

public class GetRoleService {
	static Logger log =  Logger.getLogger(GetRoleService.class);
	public static  synchronized  RoleBean  getRole(String name){
		
		
	    RaduistClient client = new RaduistClient();
	    client.setdataParse(new DataParseService());
	    boolean isConn = client.isConnect(CommData.radiusIp, CommData.radiusPort);
	    if (!isConn) {
	    	log.error("raduis服务器不可连接");
	    	return null;
	    }
	    try {
			client.connect(CommData.radiusIp, CommData.radiusPort);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		RoleBean user = null;
		 boolean isLogin = false;
		try {
			isLogin = client.login(CommData.radiusNumber, CommData.radiusPwd);
			 if (!isLogin) {
			    	log.error("raduis服务器登录失败number="+CommData.radiusNumber+" pwd="+CommData.radiusPwd);
			    	return null;
			    } 
			    user = client.queryuser(name);
			    
			    if (user == null) {
			    	log.error("raduis服务器获取账号信息失败name="+name);
			    	return null;
			    }
			    Order order = client.queryorder(name);
			    user.setOrder(order);
		} catch (Exception e) {
			log.error("raduis信息异常:",e);
		}finally {
			if (isLogin) {
				client.logout(CommData.radiusNumber);
			}
		}
		return user;

	}
	
}
